package com.gtworldmobile.tabs;

import org.mixare.R;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ScrollView;
import android.widget.Spinner;


public class MiCuentaPrefer extends Fragment {

	private SharedPreferences.Editor editor;
	private boolean default_value_home;
	private boolean default_value_cant;
	private SharedPreferences prefs;
	private Spinner cantidad;
	private Spinner home;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (container == null) {

			return null;
		}
		default_value_home = false;
		default_value_cant = false;
		final ScrollView sv = (ScrollView) inflater.inflate(
				R.layout.mi_cuenta_pref, container, false);
		prefs = this.getActivity().getSharedPreferences("preferencias",
				Context.MODE_PRIVATE);
		home = (Spinner) sv.findViewById(R.id.pantallaInicio);
		home.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int pos, long id) {
				if (default_value_home) {
					editor = prefs.edit();
					editor.putString("home", String.valueOf(pos + 1));
					editor.commit();

					/*
					 * Toast.makeText( parent.getContext(),
					 * "Cambios Guardados: " +
					 * parent.getItemAtPosition(pos).toString(),
					 * Toast.LENGTH_LONG).show();
					 */
					// default_value_home = false;

				} else {
					home.setSelection(Integer.valueOf(prefs.getString("home",
							"1")) - 1);
					default_value_home = true;
				}

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
		cantidad = (Spinner) sv.findViewById(R.id.numElementos);
		cantidad.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int pos, long id) {

				if (default_value_cant) {

					editor = prefs.edit();
					editor.putString("cantidad", parent.getItemAtPosition(pos)
							.toString());
					editor.commit();

					/*
					 * Toast.makeText( parent.getContext(),
					 * "Cambios Guardados: " +
					 * parent.getItemAtPosition(pos).toString(),
					 * Toast.LENGTH_LONG).show();
					 */
					// default_value_cant = false;

				} else {
					int c = Integer.valueOf(prefs.getString("cantidad", "5"));
					if (c == 5) {
						cantidad.setSelection(0);
					} else if (c == 10) {
						cantidad.setSelection(1);
					} else {
						cantidad.setSelection(2);
					}
					default_value_cant = true;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
		return sv;
	}
}
